<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Email;

/**
 * 资源列表管理
 * Class User
 * @package app\worker\controller
 */
class Body extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'Body';

    /**
     * 资源列表管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '资源列表管理';
        $query = $this->_query($this->table)
            ->dateBetween('create_at')
            ->where(['is_deleted' => '0']);
        $query->like('body_name')
            ->equal('status')
            ->order('create_at desc')
            ->page();
    }

    /**
     * 添加主体信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 修改主体信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        if(request()->isPost()){
            //主体名称不能重复
            if(!isset($data['id'])){
                if(Db::name($this->table)->where('body_name', $data['body_name'])->where('is_deleted', 0)->find()){
                    $this->error('该主体名称已存在');
                }
            }
        }
    }

    /**
     * 更改主体信息状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $this->_save($this->table, ['status' => input('status', '0')]);
    }

    /**
     * 删除主体信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }
}
